import { _decorator, Color, Component, director, find, Label, Node, resources, sp, Sprite, SpriteFrame, ToggleContainer, Tween, tween, v3 } from 'cc';
import { UILayerMgr } from './UILayerMgr';
import { UserInfo } from './UserInfo';
import { StartSceneGameMgr } from './StartSceneGameMgr';
import { AudioMgr } from '../utils/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('Start')
export class Start extends Component {

    @property(Node)
    roleNode: Node;
    currSelectRoleSp:sp.Skeleton;
    currSelectPetSp:Node;
    turntableBtn:Node;
    roleBtn:Node;

    bottomToggleGroup:ToggleContainer;

    static ins:Start;

    protected onLoad(): void {
        Start.ins=this;
        this.bottomToggleGroup=find("Canvas/bottomUIRoot/bg/ToggleGroup").getComponent(ToggleContainer);
        this.currSelectRoleSp=find("Canvas/homePanel/roleNode/sp").getComponent(sp.Skeleton);
        this.turntableBtn=find("Canvas/homePanel/turntableBtn");
        this.roleBtn=find("Canvas/homePanel/roleBtn");
        this.currSelectPetSp=find("Canvas/homePanel/petNode/petsp");
        director.addPersistRootNode(find("PersistRootNode"));

    }

    protected start(): void {
        if (AudioMgr.ins.isAudioBgm) {
        AudioMgr.ins.play("audio/bgm0",1);
        }
        else{
            AudioMgr.ins.stop();
        }
        this.initHomePanel();

    }

    initHomePanel() {//初始化主界面
        //更新上阵角色
        this.currSelectRoleSp.node.active = false;
        let role = UserInfo.hasRoleDic.get(UserInfo.currGoBattleRoleId)

        if (role.roleType == 5) {
            this.currSelectRoleSp.node.scale = v3(0.2, 0.2, 1)
        }
        else {
            this.currSelectRoleSp.node.scale = v3(1, 1, 1)
        }

        console.log(UserInfo.currGoBattleRoleId,"UserInfo.currGoBattleRoleId")
        console.log(role,"role")
        resources.loadDir(`${role.roleTp.spinePath}`, sp.SkeletonData, (err, data) => {
            if (err) {
                console.log(err);
                return;
            } else {
                let comp = this.currSelectRoleSp;
                comp.skeletonData = data[0];
                comp.setAnimation(0, "Standby", true);
            }
        })

        setTimeout(() => {
            this.currSelectRoleSp.node.active = true;
        }, 100);

     
        this.roleNode.getChildByName("Label").getComponent(Label).string="当前上阵:"+UserInfo.hasRoleDic.get(UserInfo.currGoBattleRoleId).roleTp.name;

        //更新上阵宠物
        let goBattlePet = StartSceneGameMgr.ins.getGoBattlePet();


        if (goBattlePet) {
          this.currSelectPetSp.active = true;
          resources.load(`${goBattlePet.petTp.imgPath}/spriteFrame`,SpriteFrame,(err,res)=>{
            this.currSelectPetSp.getComponent(Sprite).spriteFrame=res;
          })
            
          tween(this.currSelectPetSp)
          .tag(46)
          .to(0.4,{scale:v3(1,1.1,1),position:v3(0,30,0)})
          .to(0.4,{scale:v3(1,1,1),position:v3(0,0,0)})
          .union()
          .repeatForever()
          .start()
        }
        else{
            this.currSelectPetSp.active = false;
        }
  

        //更新按钮ui动效
        Tween.stopAllByTag(45);
        tween(this.turntableBtn.getChildByName("spin_1"))
        .tag(45)
        .by(1.3,{angle:360})
        .repeatForever()
        .start()

        tween(this.roleBtn)
        .tag(45)
        .to(0.5,{scale:v3(1.1,1,1)})
        .to(0.5,{scale:v3(1,1.1,1)})
        .to(1,{scale:v3(1,1,1)})
        .union()
        .repeatForever()
        .start()

    }

    onclickselectGateGameBtnBtn() {
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);
        if (UILayerMgr.getIns.isInitAlready == false) {
            console.log("ui层还未初始化成功")
            return
        }
        UILayerMgr.getIns.openUILayer("selectGatePanel");
    }

    onclickStarGameBtn() {
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);;
        StartSceneGameMgr.ins.sceneGateLevel=UserInfo.level;
        director.loadScene("main")
    }

    onclickEveryDayTaskBtn(){
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        if (UILayerMgr.getIns.isInitAlready == false) {
            console.log("ui层还未初始化成功")
            return
        }
        UILayerMgr.getIns.openUILayer("everyDayTaskPanel");
    }

    onclickTurntablePanelBtn(){
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        if (UILayerMgr.getIns.isInitAlready == false) {
            console.log("ui层还未初始化成功")
            return
        }
        UILayerMgr.getIns.openUILayer("turntablePanel");
    }

    onclickSignPanelBtn(){
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        if (UILayerMgr.getIns.isInitAlready == false) {
            console.log("ui层还未初始化成功")
            return
        }
        UILayerMgr.getIns.openUILayer("everyDaySignPanel");
    }

    onclickShopBtn(){
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        if (UILayerMgr.getIns.isInitAlready == false) {
            console.log("ui层还未初始化成功")
            return
        }

        UILayerMgr.getIns.openUILayer("shopPanel")
    }
    
    onclickAchievementBtn(){
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        if (UILayerMgr.getIns.isInitAlready == false) {
            console.log("ui层还未初始化成功")
            return
        }

        UILayerMgr.getIns.openUILayer("achievementsPanel")
    }

    onclickRoleBtn(){
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        if (UILayerMgr.getIns.isInitAlready == false) {
            console.log("ui层还未初始化成功")
            return
        }

        UILayerMgr.getIns.openUILayer("equipPanel")
    }

    onclickPetBtn(){
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        if (UILayerMgr.getIns.isInitAlready == false) {
            console.log("ui层还未初始化成功")
            return
        }

        UILayerMgr.getIns.openUILayer("petPanel")
    }

    onclickSetBtn(){
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        if (UILayerMgr.getIns.isInitAlready == false) {
            console.log("ui层还未初始化成功")
            return
        }

        UILayerMgr.getIns.openUILayer("setPanel")
    }


    onclickBottomToggleGroup() {
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        if (UILayerMgr.getIns.isInitAlready == false) {
            console.log("ui层还未初始化成功")
            return
        }

        let clickIndex;
        for (const key in this.bottomToggleGroup.toggleItems) {
            if (this.bottomToggleGroup.toggleItems[key].isChecked == true) {
                clickIndex = key;
            }
        }

        UILayerMgr.getIns.closeAllLayer();
        this.bottomToggleGroup.node.children[0].getChildByName("Label").getComponent(Label).color=Color.WHITE
        this.bottomToggleGroup.node.children[1].getChildByName("Label").getComponent(Label).color=Color.WHITE
        this.bottomToggleGroup.node.children[2].getChildByName("Label").getComponent(Label).color=Color.WHITE


        switch (clickIndex) {
            case "0":
                UILayerMgr.getIns.openUILayer("equipPanel")
                this.bottomToggleGroup.node.children[0].getChildByName("Label").getComponent(Label).color=Color.RED
                break;
            case "1":
                this.initHomePanel();
                this.bottomToggleGroup.node.children[1].getChildByName("Label").getComponent(Label).color=Color.RED
                break;
            case "2":
                UILayerMgr.getIns.openUILayer("shopPanel")
                this.bottomToggleGroup.node.children[2].getChildByName("Label").getComponent(Label).color=Color.RED
                break;

        }


    }



}


